package cn.wizzer.app.web.modules.controllers.platform.user;

import cn.wizzer.app.sys.modules.models.SysMsg;
import cn.wizzer.app.sys.modules.services.SysMsgService;
import cn.wizzer.app.user.modules.models.UserCertify;
import cn.wizzer.app.user.modules.services.UserCertifyService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.utils.StringUtil;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/user/certify")
public class CertifyController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private UserCertifyService userCertifyService;

    @Inject
    @Reference
    private UserService userService;

    @Inject
    @Reference
    private SysMsgService userMsgService;

    @Inject
    private RedisService redisService;

    @At("/")
    @Ok("beetl:/platform/user/view/certify/index.html")
    @RequiresPermissions("user.certified")
    public void index() {
    }

    /**
     * 查询（分页）
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.certified")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("status") String status,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(userCertifyService.listPage(pageNumber, pageSize, searchKeyword, searchDate, status));
    }
    /**
     * 审核
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.certified")
    public Object edit(String id,boolean result) {
        UserCertify fetch = userCertifyService.fetch(id);
        userCertifyService.update(Chain.make("status",result?1:-1),Cnd.where("id","=",id));
        //构建消息通知
        SysMsg userMsg = userMsgService.createMsg(null, "认证审核提醒", "<p>审核" + (result ? "通过" : "失败") + "</p>", StringUtil.getPlatformUid());
        userMsgService.saveMsg(userMsg, new String[]{fetch.getUserId()});
        //修改用户表
        if (result){
            userService.update(Chain.make("certified",1),Cnd.where("id","=",fetch.getUserId()));
        }
        return Result.success();
    }

}
